﻿using M_Quality.API.Application.Command.QualityPlanCommand;
using M_Quality.Domain;
using M_Quality.Domain.Dto;
using M_Quality.Repository.Interface;
using MediatR;

namespace M_Quality.API.Application.CommandHandler.QualityPlanCommandHandler
{
    /// <summary>
    /// 显示质检方案信息
    /// </summary>
    public class QualityPianListCommandHandler : IRequestHandler<QualityPianListCommand, PageDto<QualityPlan>>
    {
        protected readonly IQualityPlanRepositry _qualityPlanRepositry;

        public QualityPianListCommandHandler(IQualityPlanRepositry qualityPlanRepositry)
        {
            _qualityPlanRepositry = qualityPlanRepositry;
        }
        public async Task<PageDto<QualityPlan>> Handle(QualityPianListCommand request, CancellationToken cancellationToken)
        {
            var PlanList = await _qualityPlanRepositry.GetAsync(d => !d.IsDeleted);
            if (!string.IsNullOrEmpty(request.QualityPlanCode))
            {
                PlanList= PlanList.Where(d=>d.QualityPlanCode.Contains(request.QualityPlanCode)).ToList();
            }
            if (!string.IsNullOrEmpty(request.QualityPlanSart))
            {
                PlanList= PlanList.Where(d=>d.QualityPlanSart==request.QualityPlanSart).ToList();
            }
            PageDto<QualityPlan> page = new PageDto<QualityPlan>();
            page.TotalCount = PlanList.Count();
            page.PageCount = (int)Math.Ceiling(page.TotalCount * 1.0 / (int)request.Pagesize);
            page.list = PlanList.OrderByDescending(x => x.CreatedDate).Skip((int)(request.Pageindex - 1) * (int)request.Pagesize).Take((int)request.Pagesize).ToList();
            return page;
        }
    }
}
